# Copyright 2019 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

load("//build_tools/bazel:enforce_glob.bzl", "enforce_glob")
load("//build_tools/bazel:iree_check_test.bzl", "iree_check_single_backend_test_suite")

package(
    features = ["layering_check"],
    licenses = ["notice"],  # Apache 2.0
)

ALL_SRCS = enforce_glob(
    [
        "abs.mlir",
        "add.mlir",
        "arithmetic_right_shift.mlir",
        "bitwise_and.mlir",
        "bitwise_or.mlir",
        "bitwise_xor.mlir",
        "ceil.mlir",
        "clamp.mlir",
        "clz.mlir",
        "const.mlir",
        "equal.mlir",
        "exp.mlir",
        "floor.mlir",
        "gather.mlir",
        "greater.mlir",
        "greater_equal.mlir",
        "if.mlir",
        "log.mlir",
        "logical_left_shift.mlir",
        "logical_right_shift.mlir",
        "logical_right_shift_16.mlir",
        "matmul.mlir",
        "max_pool.mlir",
        "maximum.mlir",
        "minimum.mlir",
        "mul.mlir",
        "mul_shift.mlir",
        "negate.mlir",
        "pad.mlir",
        "reciprocal.mlir",
        "reduce.mlir",
        "reshape.mlir",
        "rsqrt.mlir",
        "select.mlir",
        "sigmoid.mlir",
        "sub.mlir",
        "table.mlir",
        "tanh.mlir",
        "transpose.mlir",
        "while.mlir",
    ],
    include = ["*.mlir"],
)

iree_check_single_backend_test_suite(
    name = "check_llvm-cpu_local-task",
    srcs = ALL_SRCS,
    compiler_flags = ["--iree-llvmcpu-target-cpu=generic"],
    driver = "local-task",
    target_backend = "llvm-cpu",
)

iree_check_single_backend_test_suite(
    name = "check_vmvx_local-task",
    srcs = ALL_SRCS,
    driver = "local-task",
    target_backend = "vmvx",
)

iree_check_single_backend_test_suite(
    name = "check_vmvx_local-sync_microkernels",
    srcs = ALL_SRCS,
    compiler_flags = [
        "--iree-vmvx-enable-microkernels",
    ],
    # Sync has more strict runtime error checking for mis-compiled programs.
    driver = "local-sync",
    target_backend = "vmvx",
)

iree_check_single_backend_test_suite(
    name = "check_vulkan-spirv_vulkan",
    srcs = ALL_SRCS,
    driver = "vulkan",
    target_backend = "vulkan-spirv",
)

iree_check_single_backend_test_suite(
    name = "check_metal-spirv_metal",
    srcs = ALL_SRCS,
    driver = "metal",
    target_backend = "metal-spirv",
)

ROCM_AND_CUDA_SRCS = enforce_glob(
    [
        "abs.mlir",
        "add.mlir",
        "arithmetic_right_shift.mlir",
        "bitwise_and.mlir",
        "bitwise_or.mlir",
        "bitwise_xor.mlir",
        "ceil.mlir",
        "clamp.mlir",
        "clz.mlir",
        "const.mlir",
        "equal.mlir",
        "exp.mlir",
        "floor.mlir",
        "gather.mlir",
        "greater.mlir",
        "greater_equal.mlir",
        "if.mlir",
        "log.mlir",
        "logical_left_shift.mlir",
        "logical_right_shift.mlir",
        "logical_right_shift_16.mlir",
        "matmul.mlir",
        "max_pool.mlir",
        "maximum.mlir",
        "minimum.mlir",
        "mul.mlir",
        "negate.mlir",
        "pad.mlir",
        "reciprocal.mlir",
        "reduce.mlir",
        "reshape.mlir",
        "rsqrt.mlir",
        "select.mlir",
        "sigmoid.mlir",
        "sub.mlir",
        "table.mlir",
        "tanh.mlir",
        "transpose.mlir",
        "while.mlir",
    ],
    include = ["*.mlir"],
    exclude = [
        "mul_shift.mlir",  # error: cannot be converted to LLVM IR: missing `LLVMTranslationDialectInterface` registration for dialect for op: tosa.apply_scale
    ],
)

iree_check_single_backend_test_suite(
    name = "check_cuda_graph",
    srcs = ROCM_AND_CUDA_SRCS,
    compiler_flags = [
        # TODO(#13984): memset emulation required for graphs.
        "--iree-stream-emulate-memset",
    ],
    driver = "cuda",
    runner_args = ["--cuda_use_streams=false"],
    tags = [
        # CUDA cuInit fails with sanitizer on.
        "noasan",
        "nomsan",
        "notsan",
        "noubsan",
        "requires-gpu-nvidia",
    ],
    target_backend = "cuda",
)

iree_check_single_backend_test_suite(
    name = "check_cuda_stream",
    srcs = ROCM_AND_CUDA_SRCS,
    driver = "cuda",
    runner_args = ["--cuda_use_streams=true"],
    tags = [
        # CUDA cuInit fails with sanitizer on.
        "noasan",
        "nomsan",
        "notsan",
        "noubsan",
        "requires-gpu-nvidia",
    ],
    target_backend = "cuda",
)

iree_check_single_backend_test_suite(
    name = "check_rocm_hip_stream",
    srcs = ROCM_AND_CUDA_SRCS,
    driver = "hip",
    runner_args = [
        "--hip_use_streams=true",
    ],
    target_backend = "rocm",
)
